package com.example.es.myapplication;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SearchView;

import com.example.zqm.neu612.Adapter.SearchAdapter;
import com.example.zqm.neu612.JavaBean.Food;
import com.example.zqm.neu612.Model.FoodModel;
import com.example.zqm.neu612.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SearchFragment extends Fragment {
  public Button button;
    public EditText editText;
    String search;
    public List<Food> foodss;
    public RecyclerView recyclerview;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, container, false);
        recyclerview = (RecyclerView) view.findViewById(R.id.search);
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

        int id = view.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);

        button = (Button)view. findViewById(R.id.sousuo);
        editText = (EditText )view.findViewById(R.id.ffdd);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSearchData();
            }
        });
        return view;
    }
    private void getSearchData() {
        search = editText.getText().toString();
        FoodModel foodModel = new FoodModel();
        Call<List<Food>> call = foodModel.getFoodBySearch(search);
        call.enqueue(new Callback<List<Food>>() {
            @Override
            public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
                foodss = response.body();
                recyclerview.setAdapter(new SearchAdapter(getActivity(), foodss));
            }
            @Override
            public void onFailure(Call<List<Food>> call, Throwable t) {

            }
        });


    }

}
